package com.romotc.qqrobot.webqq.qqInterfaceCatching;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import org.json.JSONException;

import com.romotc.common.utils.RandomString;
import com.romotc.joyin.bean.MemberCustService;
import com.romotc.qqrobot.bean.MemberCustServiceService;
import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.bean.RobotUser;
import com.romotc.qqrobot.bean.RobotUserService;
import com.romotc.qqrobot.webqq.WebQQConstant;
import com.romotc.scache.RequestSpy;
import com.romotc.scache.WebConnection;
/**
 * 点击登录 
 * 1.检查是否为虚拟号（@后的部分在robot_user表中有注册）
 * 		如果是虚拟号检查子号(@前的部分e.g 1001)是否存在 其密码是否与输入一致
 * 		如果不是虚拟号则交由腾讯处理
 * Get：http://ptlogin2.qq.com/login?u=1586030808&p=F244CD0BC9697458556B857CFF343979&verifycode=!YIJ&webqq_type=10&remember_uin=1&login2qq=1&aid=1003903&u1=http%3A%2F%2Fweb.qq.com%2Floginproxy.html%3Flogin2qq%3D1%26webqq_type%3D10&h=1&ptredirect=0&ptlang=2052&from_ui=1&pttype=1&dumy=&fp=loginerroralert&action=2-0-93348&mibao_css=m_webqq
 * 
 * @author Administrator 对于u参数为1001@5798.cc格式的内容进行拦截 检查密码是否正确
 * 
 * 返回内容格式ptuiCB('0','0','http://web.qq.com/loginproxy.html?login2qq=1&webqq_type=10','0','登录成功！');
 * 
 * 密码对照:dddd:050762F1B851A1EC7D4330E305C92D94
 */
public class Ptlogin2Login_Catching extends QQUrlBodyCatching {

	@Override
	public boolean modifyResponse(WebConnection wc, RequestSpy rqSpy)
			throws IOException {
		return false;
		
	}

	private String Key_Req_Param_Uin = "u";
	private String Key_Req_Param_pswd = "p";
	
	//局部变量
	private String Key_LocalVar_RobotUser = "RobotUser";
	private String Key_LocalVar_MemberCustService = "MemberCustService";
	private String Key_LocalVar_QQLogedin = "ExistQQLogedin";

	@Override
	String getHackReqBodyStr(RequestSpy requestSpy ,Map<String, Object> var) {
		try {
			String loginUin = requestSpy.getReqInfoParamJson().getString(Key_Req_Param_Uin );
			String pswd = requestSpy.getReqInfoParamJson().optString(Key_Req_Param_pswd );
			if(WebQQConstant.isRobotUin(loginUin)) {//if这里只检查是否包含@
				//截取1001@5798.cc @前后的部分
				int idx = loginUin.indexOf("@");
				//5798.cc
				String robotUserName = loginUin.substring(idx + 1);
				//是否为注册的机器人用户
				RobotUser robotUser = RobotUserService.qryUserByUserName(robotUserName);
				if(robotUser == null)
					return null;//不是机器人用户直接返回 交由腾讯处理
				
				//子账号是否存在
				//1001
				String custServiceId = loginUin.substring(0, idx);
				MemberCustService memberCust = MemberCustServiceService
												.qryMemberCustService(robotUser.getId(), custServiceId);
				
				if(memberCust == null)
				{
					//不存在的用户
					return "ptuiCB('3','0','','0','您输入的帐号不正确，请重新输入。');";
				}else if(!pswd.equals(memberCust.getPswd())) {
					//密码不对
					return "ptuiCB('3','0','','0','您输入的密码不正确，请重新输入。');";
				}else {
					//登录成功
					var.put(Key_LocalVar_RobotUser, robotUser);
					var.put(Key_LocalVar_MemberCustService, memberCust);
					return "ptuiCB('0','0','http://web.qq.com/loginproxy.html?login2qq=1&webqq_type=10','0','登录成功！');";
				}
			}else {
				//活跃的QQlogedin 不需要登录
				QQLogedin curQQ = QQLogedin.getCurQqLogedIn(loginUin);
				if(curQQ != null && curQQ.active)
				{
					//登录成功
					var.put(Key_LocalVar_QQLogedin , curQQ);
					return "ptuiCB('0','0','http://web.qq.com/loginproxy.html?login2qq=1&webqq_type=10','0','登录成功！');";
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	boolean isGoOn(RequestSpy requestSpy) {
		return false;
	}

	@Override
	ArrayList<String> hackRequestHeader(RequestSpy requestSpy,int bodySize, Map<String, Object> var) {
		
		ArrayList<String> headers = new ArrayList<String>();
		headers.add("Cache-Control:no-cache; must-revalidate");
		headers.add("Connection:Close");
		headers.add("Content-Type:application/x-javascript; charset=utf-8");
		headers.add("Date:" + "Wed, 25 Jan 2012 09:00:29 GMT");// RomUndo
		headers.add("P3P:CP=\"CAO PSA OUR\"");
		headers.add("Pragma:no-cache");
		headers.add("Content-Length: " + bodySize);
		headers.add("Server:Tencent Login Server/9.0.0");
		
		RobotUser user = (RobotUser)var.get(Key_LocalVar_RobotUser);
		MemberCustService memCustSv = (MemberCustService)var.get(Key_LocalVar_MemberCustService);
		
		QQLogedin curq = (QQLogedin)var.get(Key_LocalVar_QQLogedin);
		
		if(user != null && memCustSv != null) {
			headers.add("Set-Cookie: pt2gguin=o" + memCustSv.getId() + "; EXPIRES=Fri, 02-Jan-2020 00:00:00 GMT; PATH=/; DOMAIN=qq.com;");
			headers.add("Set-Cookie: uin=o" + memCustSv.getId() + "; PATH=/; DOMAIN=qq.com;");
			headers.add("Set-Cookie: clientuin=; EXPIRES=Fri, 02-Jan-1970 00:00:00 GMT; PATH=/; DOMAIN=qq.com;");
			headers.add("Set-Cookie: clientkey=; EXPIRES=Fri, 02-Jan-1970 00:00:00 GMT; PATH=/; DOMAIN=qq.com;");
			headers.add("Set-Cookie: ptisp=cm; PATH=/; DOMAIN=qq.com;");
			headers.add("Set-Cookie: ptuserinfo=6f70656e646576; PATH=/; DOMAIN=ptlogin2.qq.com;");
			headers.add("Set-Cookie: show_id=; PATH=/; DOMAIN=qq.com;");
			headers.add("Set-Cookie: ptwebqq=" + getPtwebqq() + "; PATH=/; DOMAIN=qq.com;");
		}else if(curq != null){
			
			headers.add("Set-Cookie: pt2gguin=o" + curq.getUin() + "; EXPIRES=Fri, 02-Jan-2020 00:00:00 GMT; PATH=/; DOMAIN=qq.com;");
			headers.add("Set-Cookie: uin=o" + curq.getUin() + "; PATH=/; DOMAIN=qq.com;");
			headers.add("Set-Cookie: clientuin=; EXPIRES=Fri, 02-Jan-1970 00:00:00 GMT; PATH=/; DOMAIN=qq.com;");
			headers.add("Set-Cookie: clientkey=; EXPIRES=Fri, 02-Jan-1970 00:00:00 GMT; PATH=/; DOMAIN=qq.com;");
			headers.add("Set-Cookie: ptisp=cm; PATH=/; DOMAIN=qq.com;");
			headers.add("Set-Cookie: ptuserinfo=6f70656e646576; PATH=/; DOMAIN=ptlogin2.qq.com;");
			headers.add("Set-Cookie: show_id=; PATH=/; DOMAIN=qq.com;");
			headers.add("Set-Cookie: ptwebqq=" + curq.getPtwebqq() + "; PATH=/; DOMAIN=qq.com;");
		}

		
		
		/**
		 * Cache-Control:no-cache; must-revalidate Connection:Close
		 * Content-Type:application/x-javascript; charset=utf-8 Date:Wed, 25 Jan
		 * 2012 09:00:29 GMT P3P:CP="CAO PSA OUR" Pragma:no-cache Server:Tencent
		 * Login Server/2.0.0 Set-Cookie:pt2gguin=o1586030808; EXPIRES=Fri,
		 * 02-Jan-2020 00:00:00 GMT; PATH=/; DOMAIN=qq.com; uin=o1586030808;
		 * PATH=/; DOMAIN=qq.com; skey=@anozt6hZw; PATH=/; DOMAIN=qq.com;
		 * clientuin=; EXPIRES=Fri, 02-Jan-1970 00:00:00 GMT; PATH=/;
		 * DOMAIN=qq.com; clientkey=; EXPIRES=Fri, 02-Jan-1970 00:00:00 GMT;
		 * PATH=/; DOMAIN=qq.com; zzpaneluin=; EXPIRES=Fri, 02-Jan-1970 00:00:00
		 * GMT; PATH=/; DOMAIN=qq.com; zzpanelkey=; EXPIRES=Fri, 02-Jan-1970
		 * 00:00:00 GMT; PATH=/; DOMAIN=qq.com; ptisp=cm; PATH=/; DOMAIN=qq.com;
		 * ptuserinfo=6f70656e646576; PATH=/; DOMAIN=ptlogin2.qq.com; show_id=;
		 * PATH=/; DOMAIN=qq.com;
		 * ptwebqq=137daa31e381ac17d3e02d38a1ddd0ff0265c496ae8dd64cd087e5059c7a02c5;
		 * PATH=/; DOMAIN=qq.com;
		 */
		return headers;
	}

	private String getPtwebqq() {
		String ptwebqq = RandomString.randomString(65);
		
//		//保存到数据库中供  不需要保存 该值在cookie中有
//		QQLogedin qqLogedin = new QQLogedin();
//		qqLogedin.setPtwebqq(ptwebqq);
//		qqLogedin.Save();
		
		return ptwebqq;
	}
	

}
